package com.szosen.util;

import android.annotation.SuppressLint;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class TimeHelper {
    public static final long MILLSEC_DAY = 1000 * 60 * 60 * 24;
    // public static final String FORMAT = "yyyy/MM/dd HH:mm:ss";
    public static final String FORMAT = "yyyy-MM-dd HH:mm:ss";
    // public static final String DATE_FORMAT = "yyyy/MM/dd";
    public static final String DATE_FORMAT = "yyyy-MM-dd";
    private static DateFormat formater = new SimpleDateFormat(FORMAT,
            Locale.CHINA);
    private static DateFormat Date_formater = new SimpleDateFormat(DATE_FORMAT,
            Locale.CHINA);

    /**
     * 判断前面和后面的大小。
     *
     * @param day1
     * @param day2
     * @return
     */
    public static int getDifferentDay(String day1, String day2) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date2 = format.parse(day1);
            Date date = format.parse(day2);
            System.out.println("两个日期的差距：" + differentDays(date, date2));
            return differentDays(date, date2);
        } catch (ParseException e) {
            e.printStackTrace();
            return -1;
        }
    }

    /**
     * date2比date1多的天数
     *
     * @param date1
     * @param date2
     * @return
     */
    public static int differentDays(Date date1, Date date2) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        int day1 = cal1.get(Calendar.DAY_OF_YEAR);
        int day2 = cal2.get(Calendar.DAY_OF_YEAR);

        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);
        if (year1 != year2)   //同一年
        {
            int timeDistance = 0;
            for (int i = year1; i < year2; i++) {
                if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0)    //闰年
                {
                    timeDistance += 366;
                } else    //不是闰年
                {
                    timeDistance += 365;
                }
            }

            return timeDistance + (day2 - day1);
        } else    //不同年
        {
            System.out.println("判断day2 - day1 : " + (day2 - day1));
            return day2 - day1;
        }
    }

    /***
     * 检查时间是否符合正则表达式2016-09-14
     *
     * @param time
     * @return
     */

    public static boolean checkUploadTimeOK(String time) {
        return time.matches("\\d{4}-\\d{2}-\\d{2}");
    }

    /***
     * 获取day天之后的时间值。
     * @param d
     * @param day
     * @return
     */
    public static Date getDateAfter(Date d, int day) {
        Calendar now = Calendar.getInstance();
        now.setTime(d);
        now.set(Calendar.DATE, now.get(Calendar.DATE) + day);
        return now.getTime();
    }

    /***
     * 放入date 返回格式为"yyyy-MM-dd HH:mm:ss"的字符串
     *
     * @param date
     * @return
     */
    public static String getTimeByDate(Date date) {
        if (date == null) {
            return "";
        }
        return formater.format(date).toString();
    }

    /***
     * 获取15天后的时间值，
     * @return
     */
    public static String getAfter15DaysDate() {
        SimpleDateFormat sdformat = new SimpleDateFormat("yyyy-MM-dd");
        Date now = new Date();
        Date after15Date = getDateAfter(now, 15);
        return sdformat.format(after15Date);
    }

    /**
     * 获取当前时间的字符串格式，示例：2016-09-14 10:43:52
     *
     * @return
     */
    public static String getCurrentTime() {
        return formater.format(new Date()).toString();
    }

    public static Date getCurrentDate() {
        return new Date();
    }

    public static String getCurrentTimeFormat(String format) {
        SimpleDateFormat sdformat = new SimpleDateFormat(format);
        return sdformat.format(new Date());
    }

    /**
     * // 获取健康体检的日期 返回的格式为：2016-10-28 的样式
     */
    @SuppressLint("SimpleDateFormat")
    public static String getJKTJCheckTime() {
        SimpleDateFormat sdformat = new SimpleDateFormat("yyyy-MM-dd");
        return sdformat.format(new Date());
    }

    /**
     * 返回的格式为：2016-10-28 的样式
     */
    @SuppressLint("SimpleDateFormat")
    public static String getJKTJCheckDate() {
        SimpleDateFormat sdformat = new SimpleDateFormat("yyyy-MM-dd");
        return sdformat.format(new Date());
    }

    /***
     * 获取一个系统时间的毫秒值，作为一体机id：checkid，也作为每个表的主键，上传到后台确保能查询主键。
     *
     * @return
     */
    public static String getMainTimeKey() {
        return System.currentTimeMillis() + "";
    }

    /***
     * 求beforeDays前的格式化日期
     *
     * @param beforeDays
     * @return
     */
    public static String getBeforeTime(int beforeDays) {
        Date current = new Date();
        long longTime = current.getTime();
        Date beforeDate = new Date(longTime - MILLSEC_DAY * beforeDays);
        return formater.format(beforeDate).toString();
    }

    public static long parseTime(String stime) throws ParseException {
        return formater.parse(stime).getTime();
    }

    /***
     * 长整型时间转化为字符串格式
     *
     * @param lTime
     * @return
     */
    public static String getStringTime(long lTime) {
        return formater.format(new Date(lTime)).toString();
    }

    /***
     * 长整型时间转化为字符串格式日期
     *
     * @param time
     * @return
     */
    public static String getStringDate(long time) {
        return Date_formater.format(new Date(time)).toString();

    }

    public static Calendar string2Calender(String stime) {
        System.out.println(stime);
        Calendar calendar = Calendar.getInstance();
        int yearEndIndex = stime.indexOf("年");
        int monthEndIndex = stime.indexOf("月");
        int dayEndIndex = stime.indexOf("日");
        int year = Integer.parseInt(stime.substring(0, yearEndIndex).trim());
        int month = Integer.parseInt(stime.substring(yearEndIndex + 1,
                monthEndIndex).trim());
        int day = Integer.parseInt(stime.substring(monthEndIndex + 1,
                dayEndIndex).trim());
        calendar.set(year, month, day, 8, 0);
        return calendar;
    }

    public static void main(String[] args) {
        System.out.println(string2Calender("2014年1月1日").toString());
    }
}
